/*!
 * This file implements a form to display the movie informations.
 * @author Mazzer Yannis, Calingarayar Martial
 * @file movie.h
 */
#include <QDebug>
#include <QWidget>
#include <QString>
#include <QDate>
#include <QTime>
#include <QList>
#include <QTreeWidget>
#include <QMap>
#include <QSqlTableModel>
#include <QSqlRecord>
#include "movie.h"
#include "ui_moviedescr.h"

Movie::Movie(QWidget *parent) : QWidget(parent)
{
    this->draw();
}

int Movie::getNumero()
{
    return this->numero;
}

QString Movie::getTitle()
{
    return this->titleLineEdit->text();
}

QString Movie::getOriginalTitle()
{
    return this->origTitleLineEdit->text();
}

QDate Movie::getReleaseDate()
{
    return this->dateDateEdit->date();
}

QString Movie::getCountry()
{
    return this->countryLineEdit->text();
}

QString Movie::getMinAge()
{
    return this->minAgeLineEdit->text();
}

//QPixmap Movie::getPicture()
//{
//    return this->picture;
//}
//
//ActorMap Movie::getCasting()
//{
//    return this->casting;
//}
//
//LangList Movie::getLang()
//{
//    return this->lang;
//}
int Movie::getIndex()
{
    return this->mapper->currentIndex();
}

void Movie::setModelIndex(const QModelIndex &index, QSqlTableModel *model)
{
    this->mod = model;
    this->record = model->record(index.row());
    setNumero(this->record.value("numero").value<int>());

    this->mapper = new QDataWidgetMapper(this);
    this->mapper->setModel(model);
    this->mapper->addMapping(this->titleLineEdit,
			     model->fieldIndex("Title"));
    this->mapper->addMapping(this->origTitleLineEdit,
			     model->fieldIndex("OriginalTitle"));
    this->mapper->addMapping(this->dateDateEdit,
			     model->fieldIndex("Date"));
    this->mapper->addMapping(this->lengthTimeEdit,
			     model->fieldIndex("Length"));
    this->mapper->addMapping(this->directorLineEdit,
			     model->fieldIndex("Director"));
    this->mapper->addMapping(this->countryLineEdit,
			     model->fieldIndex("Country"));
    this->mapper->addMapping(this->minAgeLineEdit,
			     model->fieldIndex("MinAge"));
    this->mapper->setCurrentIndex(index.row());

    castSetUp();
    langSetUp();

    connect(model, SIGNAL(modelReset()), this, SLOT(resetIndex()));
    connect(this->commitPushButton, SIGNAL(clicked()),
	    model, SLOT(submitAll()));
}

void Movie::castSetUp()
{
    castModel = new QSqlTableModel(this, mod->database());
    castModel->setTable("Cast");
    QString filter = QString("numero = %1").arg(numero);
    qDebug() << filter << endl;
    castModel->setFilter(filter);
    castModel->select();
    this->castTableView->setModel(castModel);
    this->castTableView->hideColumn(0);
    this->castTableView->hideColumn(3);
}

void Movie::langSetUp()
{
    langModel = new QSqlTableModel(this, mod->database());
    langModel->setTable("Languages");
    QString filter = QString("numero = %1").arg(numero);
    langModel->setFilter(filter);
    langModel->select();
    this->languageTreeView->setModel(langModel);
    this->languageTreeView->hideColumn(0);
    this->languageTreeView->hideColumn(2);
    this->languageTreeView->hideColumn(3);
}

void Movie::setNumero(const int &newNumero)
{
    this->numero = newNumero;
}

void Movie::setTitle(const QString &newTitle)
{
    this->titleLineEdit->setText(newTitle);
    this->record.setValue("Title", QVariant(newTitle));
}

void Movie::setOriginalTitle(const QString &newOriginalTitle)
{
    this->origTitleLineEdit->setText(newOriginalTitle);
    this->record.setValue("OriginalTitle", QVariant(newOriginalTitle));
}

void Movie::setReleaseDate(const QDate &newReleaseDate)
{
    this->dateDateEdit->setDate(newReleaseDate);
    this->record.setValue("Date", QVariant(newReleaseDate));
}

void Movie::setDirector(const QString &newDirector)
{
    this->directorLineEdit->setText(newDirector);
    this->record.setValue("Director", QVariant(newDirector));
}

void Movie::setLength(const QTime &newLength)
{
    this->lengthTimeEdit->setTime(newLength);
    this->record.setValue("Length", QVariant(newLength));
}

void Movie::setCountry(const QString &newCountry)
{
    this->countryLineEdit->setText(newCountry);
    this->record.setValue("Country", QVariant(newCountry));
}

void Movie::setMinAge(const QString &newMinAge)
{
    this->minAgeLineEdit->setText(newMinAge);
    this->record.setValue("MinAge", QVariant(newMinAge.toInt()));
}

//void Movie::setPicture(const QPixmap &newPicture)
//{
//    this->picture = newPicture;
//}
//
//void Movie::setCasting(const ActorMap &newCasting)
//{
//    this->casting = newCasting;
//}
//
//void Movie::setLang(const LangList &newLang)
//{
//    this->lang = newLang;
//}

void Movie::draw()
{
    setupUi(this);
}

void Movie::resetIndex()
{
    for(int i = 0; i <= this->mod->rowCount(); i++) {
	QVariant num = this->mod->record(i).value("numero");
	if(num == this->record.value("numero")) {
	    this->mapper->setCurrentIndex(i);
	    break;
	}
    }
}
